package com.songsong.myblog2.controller;

import com.alibaba.fastjson.JSON;
import com.songsong.myblog2.domain.Categories;
import com.songsong.myblog2.service.ArticleService;
import com.songsong.myblog2.service.CategoriesService;
import com.songsong.myblog2.util.TimeUtil;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.songsong.myblog2.util.TransCodingUtil;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/categories")
public class CategoriesController {

    //分类信息controller

    //注入categoriesService
    @Resource(name = "categoriesService")
    private CategoriesService categoriesService;

    //注入articleService
    @Resource(name = "articleService")
    private ArticleService articleService;

    //跳转到分类页面
    @RequestMapping("/toCategories")
    public String toCategories(){
        return "anon/categories";
    }

    @RequestMapping("/addCategories")
    @RequiresRoles(value = "admin")
    @ResponseBody
    public String addCategories(@RequestParam("categoriesName") String categoriesName) {
        //先查看该分类是否存在
        Categories result = categoriesService.selectOne(categoriesName);
        if (result != null) {
            return "已经存在";
        } else {
            //获取当前时间
            TimeUtil timeUtil = new TimeUtil();
            String formatDateForThree = timeUtil.getFormatDateForThree();
            //创建一个新的分类对象
            Categories categories = new Categories();
            categories.setCategoriesName(categoriesName);
            categories.setCategoriesData(formatDateForThree);
            //调用service
            categoriesService.addCategories(categories);
            return "添加成功";
        }

    }

    /**
     * 管理员获取分类管理
     *
     * @return
     */

    @GetMapping("/getCategoriesManagement")
    @ResponseBody
    @RequiresRoles(value = "admin")
    public JSONObject getCategoriesManagement(@RequestParam("rows") String rows,
                                              @RequestParam("pageNum") String pageNum) {
        return categoriesService.getCategoriesManagement(Integer.parseInt(rows), Integer.parseInt(pageNum));
    }

    /**
     * 分类状态改为禁用
     *
     * @param categoriesName
     * @return
     */
    @RequestMapping("/categoriesToStop/{categoriesName}")
    @RequiresRoles(value = "admin")
    public String categoriesToStop(@PathVariable("categoriesName") String categoriesName) {
        categoriesService.categoriesToStop(categoriesName);
        return "anon/superadmin";
    }

    /**
     * 分类状态改为启用
     *
     * @param categoriesName
     * @return
     */
    @RequestMapping("/categoriesToBegin/{categoriesName}")
    @RequiresRoles(value = "admin")
    public String categoriesToBegin(@PathVariable("categoriesName") String categoriesName) {
        categoriesService.categoriesToBegin(categoriesName);
        return "anon/superadmin";
    }

    /**
     * 获得所有分类名以及每个分类名的文章数目
     * @return
     */
    @GetMapping("/findCategoriesNameAndArticleNum")
    @ResponseBody
    public JSONObject findCategoriesNameAndArticleNum(){
        return categoriesService.findCategoriesNameAndArticleNum();
    }

    //写文章页面获取分类
    @RequestMapping("/getCategories")
    @ResponseBody
    public String getCategories(){
        List<Categories> categorieslist=categoriesService.getCategories();
        String result= JSON.toJSONString(categorieslist);
        return result;
    }

    /**
     * 分页获得该分类下的文章
     * @return
     */
    @GetMapping("/getCategoryArticle")
    @ResponseBody
    public JSONObject getCategoryArticle(@RequestParam("category") String category,
                                         HttpServletRequest request){

        try {
            category = TransCodingUtil.unicodeToString(category);
        } catch (Exception e){
        }
        int rows = Integer.parseInt(request.getParameter("rows"));
        int pageNum = Integer.parseInt(request.getParameter("pageNum"));

        return articleService.findArticleByCategory(category, rows, pageNum);
    }

}

